package com.atguigu.mybatisplusdemo.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.io.IOException;

/**
 * 自定义登录过滤器类，继承自AbstractAuthenticationProcessingFilter
 * 用于处理自定义登录请求，并进行身份验证
 */
//public class CustomLoginFilter extends AbstractAuthenticationProcessingFilter {
//
//    // 默认的请求匹配规则，用于匹配/login的POST请求
//    private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER
//            = new AntPathRequestMatcher("/login", "POST");
//
//    // 用于序列化和反序列化JSON对象
//    private final ObjectMapper objectMapper;
//
//
//    public CustomLoginFilter(ObjectMapper objectMapper, AuthenticationManager authenticationManager, AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler) {
//
//        super(DEFAULT_ANT_PATH_REQUEST_MATCHER, authenticationManager);
//        super.setAuthenticationSuccessHandler(successHandler);
//        super.setAuthenticationFailureHandler(failureHandler);
//
//        this.objectMapper = objectMapper;
//    }
//
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
//            throws AuthenticationException {
//        // 确保请求方法是POST
//        if (!request.getMethod().equals("POST")) {
//            throw new AuthenticationServiceException("不支持身份验证方法: " + request.getMethod());
//        }
//
//        try {
//            LoginRequest loginRequest = objectMapper.readValue(request.getInputStream(), LoginRequest.class);
//
//            // 验证输入数据
//            if (loginRequest == null || (loginRequest.getEmail() == null && loginRequest.getPhone() == null)) {
//                throw new BadCredentialsException("邮箱或手机号必须提供其一");
//            }
//            if (loginRequest.getEmail() != null && loginRequest.getPhone() != null) {
//                throw new BadCredentialsException("只能使用邮箱或手机号一种方式登录");
//            }
//            if (loginRequest.getPassword() == null || loginRequest.getPassword().isEmpty()) {
//                throw new BadCredentialsException("密码不能为空");
//            }
//
//            String principal = loginRequest.getEmail() != null ? loginRequest.getEmail() : loginRequest.getPhone();
//
//            UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(
//                    principal,
//                    loginRequest.getPassword()
//            );
//
//            authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
//
//            return this.getAuthenticationManager().authenticate(authRequest);
//        } catch (IOException e) {
//            throw new AuthenticationServiceException("请求解析失败", e);
//        } catch (RuntimeException e) {
//            throw new AuthenticationServiceException("登录过程中发生错误", e);
//        }
//    }
//
//}